/*
 * Converter.java
 * Convert Kilograms to Pounds (GUI Style)
 *
 * @author:  John Thomason
 *  Course:  CS223
 *  Assign:  1
 * Seminar:  5
 *    Date:  Feb 6, 2009
 */

package gui;
import javax.swing.*;
import java.awt.event.*;
import conversion.*;

/**
 *
 * @author jt
 */
public class Converter extends javax.swing.JFrame {

    /** maximum input string length */
    public static final int MAX_INPUT_LEN = 10;

    /** Creates new form Converter */
    public Converter() {

        //Initialize GUI components
        initComponents();

    //Listeners (and Handlers)
        /**----------------------------------------------------------
         * Quit Button Listener
         */
        jbtQuit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }//actionPerformed()
        });//addActionListener()

        /**----------------------------------------------------------
         * Convert Button Listener
         */
        jbtConvert.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String result = "";
                //Trim to maximum input string length
                if ((result = jtfKilo.getText()).length() > MAX_INPUT_LEN){
                    result = result.substring(0, MAX_INPUT_LEN).trim();
                    jtfKilo.setText(result);
                }//if

                //Try to convert the kilo textfield to a string 
                //representing the number of pounds
                try {
                    result = String.format("%.4f lbs.",
                             K2P.k2p(Double.parseDouble(result)));
                }//try

                //Catch an invalid kilo argument
                catch(NumberFormatException nfe){
                    result = nfe.getMessage();
                }//catch

                //Finally display the result
                finally {
                    jlbResult.setText(String.format(
                            "<html><span " +
                            "style=\"" +
                            "font-weight:800;" +
                            "color:#A00;\">" +
                            "&nbsp;%s</span></html>", result));
                }//finally
            }//actionPerformed()
        });//addActionListener()

        /**----------------------------------------------------------
         * TextField Focus Listener
         */
        jtfKilo.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e);
                //Select the contents of the textfield when it gains focus
                jtfKilo.selectAll();
            }//focusGained()
        });//addFocusListener()

    //JFrame Config Stuff
        setLocationRelativeTo(null);
        setVisible(true);

    }//Converter Constructor

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnFields = new javax.swing.JPanel();
        jlbKilo = new javax.swing.JLabel();
        jlbPound = new javax.swing.JLabel();
        jlbResult = new javax.swing.JLabel();
        jtfKilo = new javax.swing.JTextField();
        jpnButtons = new javax.swing.JPanel();
        jbtConvert = new javax.swing.JButton();
        jbtQuit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Kilos 2 Pounds");

        jlbKilo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlbKilo.setLabelFor(jtfKilo);
        jlbKilo.setText("Kilos:");
        jlbKilo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jlbPound.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlbPound.setLabelFor(jlbResult);
        jlbPound.setText("Pounds:");
        jlbPound.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jtfKilo.setText("0");

        javax.swing.GroupLayout jpnFieldsLayout = new javax.swing.GroupLayout(jpnFields);
        jpnFields.setLayout(jpnFieldsLayout);
        jpnFieldsLayout.setHorizontalGroup(
            jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnFieldsLayout.createSequentialGroup()
                        .addComponent(jlbPound, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jpnFieldsLayout.createSequentialGroup()
                        .addComponent(jlbKilo)
                        .addGap(6, 6, 6)))
                .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jlbResult, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfKilo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jpnFieldsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jlbKilo, jlbPound});

        jpnFieldsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jlbResult, jtfKilo});

        jpnFieldsLayout.setVerticalGroup(
            jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbKilo)
                    .addComponent(jtfKilo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbPound)
                    .addComponent(jlbResult))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpnFieldsLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jlbKilo, jlbPound, jlbResult, jtfKilo});

        getContentPane().add(jpnFields, java.awt.BorderLayout.CENTER);

        jpnButtons.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 8, 8, 8));
        jpnButtons.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        jbtConvert.setText("Convert");
        jpnButtons.add(jbtConvert);

        jbtQuit.setText("Quit");
        jpnButtons.add(jbtQuit);

        getContentPane().add(jpnButtons, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        //try to use the host OS PLAF
        try {
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        }//try
        catch(Exception e){
            //do nothing; default to java's plaf
        }//catch
        finally {
            //finally run the app in the event tread
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Converter();
                }//run()
            });//invokeLater()
        }//finally

    }//main()

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbtConvert;
    private javax.swing.JButton jbtQuit;
    private javax.swing.JLabel jlbKilo;
    private javax.swing.JLabel jlbPound;
    private javax.swing.JLabel jlbResult;
    private javax.swing.JPanel jpnButtons;
    private javax.swing.JPanel jpnFields;
    private javax.swing.JTextField jtfKilo;
    // End of variables declaration//GEN-END:variables

}//Converter Class
